package com.sgb.kjwl.view.ui.adapter.pick;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.BiddingEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.math.BigDecimal;
import java.util.List;

public class PickBidingAdapter extends BaseRecycleAdapter<BiddingEntity> {


    private PickHomeHotChildHeadAdapter pickHomeHotChildHeadAdapter;
    private String type = "0";
    private ImageView imgTab;
    private TextView tvTypeName;
    private RecyclerView recyclerView;
    private LinearLayout noList;
    private LinearLayout linePrice;
    private TextView priceTitle;
    private TextView priceContent;
    private IChildClick iChildClick;
    private TextView tvEndTime;
    private TextView tvAddress;

    public PickBidingAdapter(Context context, List<BiddingEntity> datas) {
        super(context, R.layout.item_pick_bidding2, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, final BiddingEntity entity) {
        holder.setBinding(BR.viewModel, entity);

        imgTab = holder.itemView.findViewById(R.id.img_tab);
        tvTypeName = holder.itemView.findViewById(R.id.tv_type_name);
        recyclerView = holder.itemView.findViewById(R.id.child_rv);
        noList = holder.itemView.findViewById(R.id.no_list);
        linePrice = holder.itemView.findViewById(R.id.line_price);
        priceTitle = holder.itemView.findViewById(R.id.price_title);
        priceContent = holder.itemView.findViewById(R.id.price_content);
        tvEndTime = holder.itemView.findViewById(R.id.tv_endTime);
        tvAddress = holder.itemView.findViewById(R.id.tv_address);

        if (2 == entity.getState()) {
            tvEndTime.setText(String.format("截止时间：%s", TextUtils.isEmpty(entity.getDeadlineTime(1)) ? "" : entity.getDeadlineTime(1)));
            imgTab.setBackground(context.getResources().getDrawable(R.mipmap.icn_jjz));
        } else if (3 == entity.getState()) {
            tvEndTime.setText(String.format("定标时间：%s", TextUtils.isEmpty(entity.getUpdatedStr()) ? "" : entity.getUpdatedStr()));
            imgTab.setBackground(context.getResources().getDrawable(R.mipmap.icn_yjs));
        } else if (5 == entity.getState()) {
//            imgTab.setBackground(context.getResources().getDrawable(R.drawable.feibiao));
            tvEndTime.setText(String.format("截止时间：%s", TextUtils.isEmpty(entity.getDeadlineTime(1)) ? "" : entity.getDeadlineTime(1)));
            imgTab.setBackground(context.getResources().getDrawable(R.mipmap.pick_tpz));
        } else if (7 == entity.getState()) {
            imgTab.setBackground(context.getResources().getDrawable(R.drawable.liubiao));
        }

//        if ("0".equals(type)) {
//            tvTypeName.setText(entity.getTypeName());
//            tvTypeName.setVisibility(View.VISIBLE);
//        } else {
//            tvTypeName.setText("");
//            tvTypeName.setVisibility(View.GONE);
//        }

        if("其他".equals(entity.getTypeName())){
            tvTypeName.setBackgroundResource(R.drawable.bg_red_5);
        }else{
            tvTypeName.setBackgroundResource(R.drawable.pick_list_red);
        }
        tvTypeName.setText(entity.getTypeName());
        tvTypeName.setVisibility(View.VISIBLE);
        tvAddress.setText(entity.getRegion() + entity.getAddress());
        if (entity.getGoodsHeaders() == null || entity.getGoodsLists() == null) {
            recyclerView.setVisibility(View.GONE);
            noList.setVisibility(View.VISIBLE);
            if (entity.getIsDisplay() == 0) {   //0、隐藏；1、展示；
                linePrice.setVisibility(View.GONE);
            } else {
                linePrice.setVisibility(View.VISIBLE);
                if (entity.getType() == 6 || entity.getType() == 7 || entity.getType() == 8 || entity.getType() == 9 || entity.getType() == 10 || entity.getType() == 13 || entity.getType() == 20) {
                    if (TextUtils.isEmpty(entity.getMinPrice())) {
                        priceTitle.setText("最低报价:  -");
                        priceContent.setText("");
                    } else {
                        priceTitle.setText("最低报价:");
//                        BigDecimal bg = new BigDecimal(entity.getMinPrice());
//                        String text  = bg.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString();
                        priceContent.setText(entity.getMinPrice() + "元");
                    }
                }
            }
        } else {
            recyclerView.setVisibility(View.VISIBLE);
            noList.setVisibility(View.GONE);
            linePrice.setVisibility(View.GONE);
            pickHomeHotChildHeadAdapter = new PickHomeHotChildHeadAdapter(context, entity.getGoodsHeaders(), entity.getGoodsLists());
            recyclerView.setLayoutManager(new LinearLayoutManager(context));
            recyclerView.setAdapter(pickHomeHotChildHeadAdapter);
            pickHomeHotChildHeadAdapter.setDisplay(entity.getIsDisplay());
            pickHomeHotChildHeadAdapter.setOnItemClick(new PickHomeHotChildHeadAdapter.OnItemClick() {
                @Override
                public void onItemClick() {
                    iChildClick.onClick(entity);
                }
            });
        }

    }

    public interface IChildClick {
        void onClick(BiddingEntity tenderListEntity);
    }

    public void onIChildClick(IChildClick iChildClick) {
        this.iChildClick = iChildClick;
    }

    public void setType(String typeNo) {
        type = typeNo;
        notifyDataSetChanged();
    }
}
